package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import po.Course;
import po.Semester;
import po.Show;
import po.Teacher;
import util.DBUtil;

public class ShowDao {
	public boolean addShow(Show show){
		Connection con = DBUtil.getConn();
		String sql = "insert into show(semester,teacher,address) values(?,?,?)";
		try {
			PreparedStatement psmt = con.prepareStatement(sql);
			psmt.setString(1, show.getSemester().getSemesterName());
			psmt.setString(2, show.getTeacher().getTeacherName());
//			psmt.setString(3, show.getCourse().getCourseName());
//			psmt.setString(4, show.getTime());
			psmt.setString(3, show.getAddress());
			psmt.executeUpdate();
			con.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	public Show getShow(String semester,String teacher,String course){
		Connection conn = DBUtil.getConn();
		String sql = "select semester,teacher,course from show where semester=? and teacher=? and course=?";
		PreparedStatement psmt;
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, semester);
			psmt.setString(2, teacher);
			psmt.setString(3, course);
			ResultSet rs = psmt.executeQuery();
			while (rs.next()) {
				Show show = new Show();
				Semester se = new Semester();
				se.setSemesterID(semester);
				show.setSemester(se);
				Teacher te = new Teacher();
				te.setTeacherID(teacher);
				show.setTeacher(te);
				Course co = new Course();
				co.setCourseID(course);
				show.setCourse(co);
				
				return show;
			}
			rs.close();
			psmt.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
//	public static void main(String[] args) {
//		CourseDao t = new CourseDao();
//		Course teacher = t.getCourse("010016");
//		System.out.println("ID:"+teacher.getCourseID());
//		System.out.println("name:"+teacher.getCourseName());
//	}

}
